import {
  createRouter,
  createWebHashHistory
} from 'vue-router';
import { useAuthStore } from "@/store";
import { storeToRefs } from "pinia";
import routes from "./routes"

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});


router.beforeEach((to, from, next) => {
  if (!to.meta.auth) {
    next();
  } else {
    const authStore = useAuthStore();
    const { token } = storeToRefs(authStore);
    if (!token.value) {
      next({ path: '/login' });
    } else {
      next();
    }
  }
})
router.afterEach(() => { })

export default router;